﻿function LabelMarker(markerid,latlng, options, map) {
    this.div_ = null;
    this.map_ = map;
    this.options_ = options;
    this.clickable_ = options.clickable ? options.clickable : true;
    this.latlng_ = latlng;
    this.markerid = markerid;
    this.setMap(map);
}
LabelMarker.prototype = new google.maps.OverlayView();
LabelMarker.prototype.onAdd = function () {
    this.div_ = jQuery("<div/>").css('border', 'none')
                           .css('border-width', '0px')
                           .css('position', 'absolute')
                           .addClass(this.options_.className)
                           .html(this.options_.innerHTML)
                           .get(0);
    jQuery(this.div_).data('data', this.options_.data).data('markerid', this.markerid);
    var panes = this.getPanes();
    panes.overlayMouseTarget.appendChild(this.div_); 

    if (this.clickable_) {

        function newEventPassthru(obj, event) {
            return function () {
                google.maps.event.trigger(obj, event)
            }
        }
        var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
        for (var i = 0; i < eventPassthrus.length; i++) {
            var name = eventPassthrus[i];
            google.maps.event.addListener(this.div_, name, newEventPassthru(this, name));
            if (this.options_[name]) {
                jQuery(this.div_)[name](this.options_[name]);
            }
        }
    }

}
LabelMarker.prototype.onRemove = function () {
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    } 
}
LabelMarker.prototype.remove = function () {
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }
    this.setMap(null);
}
LabelMarker.prototype.draw = function () { 
    var overlayProjection = this.getProjection();
    var sw = overlayProjection.fromLatLngToDivPixel(this.latlng_);  
    this.div_.style.left = sw.x + 'px';
    this.div_.style.top = sw.y + 'px';
}
LabelMarker.prototype.show = function () {
  if (this.div_) {
    this.div_.style.visibility = "visible";
  }
}
LabelMarker.prototype.hide = function () {
  if (this.div_) {
    this.div_.style.visibility = "hidden";
  }
}
LabelMarker.prototype.getLabelMarkerDiv = function () {
    return this.div_;
}
LabelMarker.prototype.setLatLng = function (latlng) {
    this.latlng_ = latlng;
    this.setLatLng(arguments);
    this.redrawLabel_()
}